package com.ccp.dev.system.controller;

import com.ccp.dev.core.basic.annotion.Action;
import com.ccp.dev.core.basic.consts.UrlConstants;
import com.ccp.dev.core.basic.internationalization.SysBaseController;
import com.ccp.dev.core.basic.model.SysAuditModelType;
import com.ccp.dev.core.basic.response.ResultData;
import com.ccp.dev.core.basic.web.query.QueryFilter;
import com.ccp.dev.core.util.BeanUtils;
import com.ccp.dev.core.util.RequestUtil;
import com.ccp.dev.core.util.StringUtil;
import com.ccp.dev.core.util.log.SysAuditThreadLocalHolder;
import com.ccp.dev.system.model.SysRole;
import com.ccp.dev.system.service.SysRoleService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 对象功能:系统角色表 控制器类
 * @author y
 */
@Controller
@Action(ownermodel = SysAuditModelType.ROLE_MANAGEMENT)
@RequestMapping(UrlConstants.PROJECT_ROOT+UrlConstants.PROJECT_MODULE_SYSTEM+"/sysRole")
public class SysRoleController extends SysBaseController {
    @Resource
    private SysRoleService sysRoleService;

    /**
     * 添加或更新系统角色表。
     * @param sysRole 添加或更新的实体
     * @return ResultData
     */
    @Action(description = "编辑角色信息",
            detail = "<#if StringUtil.isEmpty(sysRole.roleId)>添加<#else>更新</#if>角色【${sysRole.roleName}】<#if success>成功<#else>失败</#if>",
            exectype = "编辑日志")
    @ResponseBody
    @PostMapping(UrlConstants.MODULE_FUNCTION_ADD_UPDATE)
    public ResultData save(@RequestBody SysRole sysRole) {
        SysRole finalSysRole = new SysRole();
        finalSysRole.setRoleId(sysRole.getRoleId());
        finalSysRole.setRoleName(sysRole.getRoleName());
        SysAuditThreadLocalHolder.putParamerter("sysRole", finalSysRole);
        try {
            this.sysRoleService.saveRole(sysRole);
            SysAuditThreadLocalHolder.putParamerter("success", true);
            return getResultDataSuccess("save.success");
        }catch (Exception e){
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return getResultDataError(e.getMessage());
        }
    }

    /**
     * 删除角色信息
     * @param request HttpServletRequest
     * @return ResultData
     */
    @Action(description = "删除角色信息",
            detail = "删除角色<#list roleName as item>【${item}】</#list><#if success>成功<#else>失败</#if>",
            exectype = "删除日志")
    @ResponseBody
    @PostMapping(UrlConstants.MODULE_FUNCTION_DELETE)
    public ResultData delByIds(HttpServletRequest request) {
            String[] lAryId = RequestUtil.getStringAryByStr(request, "roleId");
            SysAuditThreadLocalHolder.putParamerter("roleName",sysRoleService.queryRoleNameByRoleIds(lAryId));
        try {
            this.sysRoleService.delRoleByIds(lAryId);
            SysAuditThreadLocalHolder.putParamerter("success", true);
            return getResultDataSuccess("delete.success");
        }catch (Exception e){
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return getResultDataError(e.getMessage());
        }
    }

    /**
     * 禁用或者是启用角色
     * @param roleId 角色id
     */
    @Action(description = "启用或禁用角色",
            detail = "<#assign entity=sysRoleService.getOneById(roleId)/><#if entity.enabled==enabled>启用<#else>禁用</#if>角色【${entity.roleName}】<#if success>成功<#else>失败</#if>",
            exectype = "编辑日志")
    @ResponseBody
    @PostMapping("/runEnable")
    public ResultData runEnableRole(@RequestParam("roleId") String roleId) {
        SysAuditThreadLocalHolder.putParamerter("enabled", SysRole.ENABLED_Y);
        try {
            this.sysRoleService.runEnableRole(roleId);
            SysAuditThreadLocalHolder.putParamerter("success", true);
            return getResultDataSuccess("operation.success");
        }catch (Exception e){
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return getResultDataError(e.getMessage());
        }
    }

    /**
     * 取得角色表分页列表
     * @param request HttpServletRequest
     * @return ResultData
     */
    @Action(description = "查询角色列表",
            detail = "查询列表<#if success>成功<#else>失败</#if>",
            exectype = "查询日志")
    @ResponseBody
    @GetMapping(UrlConstants.MODULE_FUNCTION_LIST)
    public ResultData list(HttpServletRequest request) {
       try{
            QueryFilter queryFilter = new QueryFilter(request);
            List<SysRole> list = this.sysRoleService.list(queryFilter);
            SysAuditThreadLocalHolder.putParamerter("success", true);
            return getResultDataSuccess("operation.success",queryFilter.getPageBean().getTotalCount(),list);
       }catch (Exception e){
           SysAuditThreadLocalHolder.putParamerter("success", false);
           return getResultDataSuccess("operation.failure");
       }
    }

    /**
     * 获取角色信息
     * @param roleId 角色id
     * @return ResultData
     */
    @Action(description = "查询角色信息",
            detail = "<#assign entity=sysRoleService.get(roleId)/>查询【${entity.roleName}】信息<#if success>成功<#else>失败</#if>",
            exectype = "查询日志")
    @ResponseBody
    @GetMapping(UrlConstants.MODULE_FUNCTION_GET)
    public ResultData get(@RequestParam("roleId") String roleId){
        try {
            SysRole sysRole = this.sysRoleService.get(roleId);
            SysAuditThreadLocalHolder.putParamerter("success", true);
            return getResultDataSuccess("operation.success",0,sysRole);
        }catch (Exception e){
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return getResultDataError("operation.failure");
        }
    }

    /**
     * 根据完整的角色别名查询角色信息
     * @param roleAlias 角色别名
     * @return ResultData
     */
    @Action(description = "根据完整的角色别名查询角色信息",
            detail = "<#if roleId?? && roleId != null><#assign entity=sysRoleService.get(roleId)/>查询【${entity.roleName}】信息<#if success>成功<#else>失败</#if></#if>",
            exectype = "查询日志")
    @ResponseBody
    @GetMapping("/getRoleByAllRoleAlias")
    public ResultData getRoleByAllRoleAlias(@RequestParam("roleAlias") String roleAlias){
        try {
            SysRole sysRole = this.sysRoleService.getRoleByAllRoleAlias(roleAlias);
            if(BeanUtils.isNotEmpty(sysRole) && StringUtil.isNotEmpty(sysRole.getRoleId())){
                SysAuditThreadLocalHolder.putParamerter("roleId", sysRole.getRoleId());
            }else{
                SysAuditThreadLocalHolder.putParamerter("roleId", null);
            }
            SysAuditThreadLocalHolder.putParamerter("success", true);
            return getResultDataSuccess("operation.success",0,sysRole);
        }catch (Exception e){
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return getResultDataError("operation.failure");
        }
    }
    /**
     * 根据角色别名查询角色信息
     * @param roleAlias 角色别名
     * @return ResultData
     */
    @Action(description = "根据角色别名查询角色信息",
            detail = "<#assign entity=sysRoleService.get(roleId)/>查询【${entity.roleName}】信息<#if success>成功<#else>失败</#if>",
            exectype = "查询日志")
    @ResponseBody
    @GetMapping("/getRoleByRoleAlias")
    public ResultData getRoleByRoleAlias(@RequestParam("roleAlias") String roleAlias,HttpServletRequest request){
        try {
            SysRole sysRole = this.sysRoleService.getRoleByRoleAlias(roleAlias,request);
            if(BeanUtils.isNotEmpty(sysRole) && StringUtil.isNotEmpty(sysRole.getRoleId())){
                SysAuditThreadLocalHolder.putParamerter("roleId", sysRole.getRoleId());
            }else{
                SysAuditThreadLocalHolder.putParamerter("roleId", null);
            }
            SysAuditThreadLocalHolder.putParamerter("success", true);
            return getResultDataSuccess("operation.success",0,sysRole);
        }catch (Exception e){
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return getResultDataError("operation.failure");
        }
    }


    /**
     * 根据用户id获取角色列表
     * @param userId 用户id
     * @return ResultData
     */
    @Action(description = "根据用户id获取角色列表",
            detail = "<#assign entity=sysUserService.getOneById(userId)/>查询【${entity.fullName}】角色列表信息<#if success>成功<#else>失败</#if>",
            exectype = "查询日志")
    @ResponseBody
    @GetMapping("/queryByUserId")
    public ResultData queryByUserId(@RequestParam("userId") String userId){
        try {
            List<SysRole> sysRoleList = this.sysRoleService.queryByUserId(userId);
            SysAuditThreadLocalHolder.putParamerter("success", true);
            return getResultDataSuccess("operation.success",0,sysRoleList);
        }catch (Exception e){
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return getResultDataError("operation.failure");
        }
    }

    /**
     * 根据用户id获取组织下角色列表
     * @return ResultData
     */
    @Action(description = "根据用户id获取组织下角色列表",
            detail = "<#assign entity=sysUserService.getOneById(userId)/>查询【${entity.fullName}】组织下角色列表信息<#if success>成功<#else>失败</#if>",
            exectype = "查询日志")
    @ResponseBody
    @GetMapping("/queryOrgRoleByUserId")
    public ResultData queryOrgRoleByUserId(HttpServletRequest request){
        try {
            QueryFilter queryFilter = new QueryFilter(request);
            List<SysRole> sysRoleList = this.sysRoleService.queryOrgRoleByUserId(queryFilter);
            SysAuditThreadLocalHolder.putParamerter("success", true);
            return getResultDataSuccess("operation.success",0,sysRoleList);
        }catch (Exception e){
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return getResultDataError("operation.failure");
        }
    }
    /**
     * 根据租户id获取角色分类
     * @param request HttpServletRequest
     * @return ResultData
     */
    @Action(description = "根据租户id获取角色分类列表",
            detail = "根据租户id获取角色分类列表<#if success>成功<#else>失败</#if>",
            exectype = "查询日志")
    @ResponseBody
    @GetMapping("/queryDistinctCategory")
    public ResultData queryDistinctCategory(HttpServletRequest request){
        try {
            String roleId = RequestUtil.getString(request,"roleId");
            String orgId = RequestUtil.getString(request,"orgId");
           List<String> stringList = this.sysRoleService.queryDistinctCategory(orgId);
            SysAuditThreadLocalHolder.putParamerter("success", true);
            return getResultDataSuccess("operation.success",0,stringList);
        }catch (Exception e){
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return getResultDataError("operation.failure");
        }
    }

    /**
     * 根据系统id获取可切换的角色
     * @return ResultData
     */
    @Action(description = "根据系统id获取可切换的角色",
            detail = "<#assign entity=subSystemService.getOneById(systemId)/>查询【${entity.sysName}】下可切换的角色<#if success>成功<#else>失败</#if>",
            exectype = "查询日志")
    @ResponseBody
    @GetMapping("/querySwitchBySystemId")
    public ResultData querySwitchBySystemId(HttpServletRequest request){
        try{
            String systemId = RequestUtil.getString(request,"systemId");
            List<SysRole> roleList = sysRoleService.querySwitchBySystemId(systemId);
            SysAuditThreadLocalHolder.putParamerter("success", true);
            return getResultDataSuccess("operation.success",0,roleList);
        }catch (Exception e){
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return getResultDataError("operation.failure");
        }
    }

    /**
     * 页面跳转 列表页
     */
    @Action(description = "角色列表页面跳转",exectype = "页面跳转日志")
    @GetMapping(UrlConstants.VIEW_DISPATCH_PREFIX+"/list")
    public String viewList(){
        return "sysRole/list";
    }
    /**
     * 页面跳转 人员分配
     */
    @Action(description = "角色分配人员页面跳转",exectype = "页面跳转日志")
    @GetMapping(UrlConstants.VIEW_DISPATCH_PREFIX+"/distributionOfStaff")
    public String viewAssignUser(HttpServletRequest request , Model model){
        String orgId = RequestUtil.getString(request,"orgId");
        String roleId = RequestUtil.getString(request,"roleId");
        model.addAttribute("roleId",roleId);
        model.addAttribute("orgId",orgId);
        return "sysRole/distributionOfStaff";
    }
    /**
     * 页面跳转 查看角色信息
     */
    @Action(description = "查看角色信息",exectype = "页面跳转日志")
    @GetMapping(UrlConstants.VIEW_DISPATCH_PREFIX+"/view")
    public String viewGetRoleInfo(HttpServletRequest request , Model model){
        String roleId = RequestUtil.getString(request,"roleId");
        model.addAttribute("roleId",roleId);
        return "sysRole/view";
    }
    /**
     * 页面跳转 新增/修改
     */
    @Action(description = "角色编辑页面跳转",exectype = "页面跳转日志")
    @GetMapping(UrlConstants.VIEW_DISPATCH_PREFIX+"/addEdit")
    public String viewAddEdit(HttpServletRequest request , Model model){
        String roleId = RequestUtil.getString(request,"roleId",null);
        String selectRoleId = RequestUtil.getString(request,"selectRoleId");
        model.addAttribute("roleId",roleId);
        model.addAttribute("selectRoleId",selectRoleId);
        return "sysRole/addEdit";
    }
}
